package com.app.service.impl;


import com.app.mapper.HelpRecordMapper;
import com.app.model.ConfigModel;
import com.app.model.HelpRecordModel;
import com.app.model.QuestionnaireInvestigationModel;
import com.app.service.ConfigService;
import com.app.service.HelpRecordService;
import com.app.service.QuestionnaireInvestigationService;
import com.app.util.Result;
import com.app.vo.HelpRecordVo;
import com.app.vo.QuestionnaireInvestigationVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @Description:零时帮扶记录
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class HelpRecordServiceImpl implements HelpRecordService {

    @Autowired
    HelpRecordMapper helpRecordMapper;

    @Autowired
    QuestionnaireInvestigationService questionnaireInvestigationService;
    @Autowired
    ConfigService configService;
    @Override
    public int insert(HelpRecordModel model) {
        return helpRecordMapper.insert(model);
    }

    @Override
    public HelpRecordVo getAllList(HelpRecordModel model) {

        QueryWrapper<HelpRecordModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<HelpRecordModel> pages = new Page<>();
        pages.setCurrent(model.getPage());
        pages.setSize(model.getLimit());
        Page<HelpRecordModel> page = helpRecordMapper.selectPage(pages, queryWrapper);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (page == null) {
            return null;
        } else {
            for (int i = 0; i < page.getRecords().size(); i++) {
                QuestionnaireInvestigationModel questionnaireInvestigationModel = questionnaireInvestigationService.getModelById(page.getRecords().get(i).getUserId());
                if (questionnaireInvestigationModel.getWishWorkTypeId() != null && questionnaireInvestigationModel.getWishWorkTypeId() > 0) {
                    ConfigModel configModel = configService.getModelById(questionnaireInvestigationModel.getWishWorkTypeId());
                    page.getRecords().get(i).setWishWorkTypeId(configModel.getName());
                }
                page.getRecords().get(i).setHelpTypeText("平台");
                page.getRecords().get(i).setOneHelpTime(format.format(Timestamp.valueOf(page.getRecords().get(i).getOneHelpTime())));
                page.getRecords().get(i).setTwoHelpTime(format.format(Timestamp.valueOf(page.getRecords().get(i).getTwoHelpTime())));
                page.getRecords().get(i).setThreeHelpTime(format.format(Timestamp.valueOf(page.getRecords().get(i).getThreeHelpTime())));
            }
            HelpRecordVo vo = new HelpRecordVo();
            vo.setList(page.getRecords());
            vo.setCount((int) page.getTotal());
            return vo;
        }
    }
    @Override
    public Integer getAllCount() {
        return helpRecordMapper.selectCount(null);
    }
    @Override
    public Integer[] getCount() {
        QueryWrapper<HelpRecordModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("one_help", "微信");
        Integer count1 = helpRecordMapper.selectCount(queryWrapper1);

        QueryWrapper<HelpRecordModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.like("two_help", "短信");
        Integer count2 = helpRecordMapper.selectCount(queryWrapper2);

        QueryWrapper<HelpRecordModel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.like("three_help", "平台");
        Integer count3 = helpRecordMapper.selectCount(queryWrapper3);
        return new Integer[]{count1, count2, count3};
    }
}
